//
//  MYSettingCell.m
//  奇闻
//
//  Created by 似水灵修 on 14-12-1.
//  Copyright (c) 2014年 MingYueGuCheng. All rights reserved.
//

#import "MYSettingCell.h"
#import "MYBaseSetting.h"
#import "MYBadgeView.h"
@interface MYSettingCell ()
@property (nonatomic, strong) UIImageView *arrowView;
@property (nonatomic, strong) MYBadgeView *badgeView;
@property (nonatomic, strong) UISwitch *switchView;
@property (nonatomic, strong) UIImageView *cheakView;
@property (nonatomic, strong) UILabel *labelView;
@end
@implementation MYSettingCell

+ (instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"MYSettingCell";
    MYSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    return cell;
}
- (UIImageView *)arrowView
{
    if (nil == _arrowView) {
        UIImageView *arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_arrow"]];
        self.arrowView = arrowView;
    }
    return _arrowView;
}
- (MYBadgeView *)badgeView
{
    if (nil == _badgeView) {
        MYBadgeView *badgeView = [[MYBadgeView alloc] init];
        self.badgeView = badgeView;
    }
    return _badgeView;
}
- (UISwitch *)switchView
{
    if (nil == _switchView) {
        UISwitch *switchView = [[UISwitch alloc] init];
        self.switchView = switchView;
    }
    return _switchView;
}
- (UIImageView *)cheakView
{
    if (nil == _cheakView) {
        UIImageView *cheakView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_checkmark"]];
        self.cheakView = cheakView;
    }
    return _cheakView;
}
- (UILabel *)labelView
{
    if (nil == _labelView) {
        _labelView = [[UILabel alloc] init];
        _labelView.textColor = [UIColor redColor];
        _labelView.textAlignment = NSTextAlignmentCenter;
       
    }
    return _labelView;
}
// 设置右边视图
- (void)setItem:(MYSettingItem *)item
{
    _item = item;
    //设置内容
    [self setupData];
    //设置右边视图
    [self setupRightView];
    //设置退出键
    [self setupExit];
}
//设置退出键
- (void)setupExit
{
    if ([_item isKindOfClass:[MYLabelItem class]]) {//文本框
        MYLabelItem *labelItem = (MYLabelItem *)_item;
        self.textLabel.hidden = YES;
        self.labelView.text = labelItem.title;
        [self addSubview:self.labelView];
    }
    else {//不是就需要干掉
        [_labelView removeFromSuperview];
    }
}
//设置内容
- (void)setupData
{
    self.textLabel.text = _item.title;
    self.detailTextLabel.text = _item.subTitle;
    self.imageView.image = _item.image;
}
//设置右边视图
- (void)setupRightView
{
    if ([_item isKindOfClass:[MYArrowItem class]]) {//箭头
        self.accessoryView = self.arrowView;
    }
    else if ([_item isKindOfClass:[MYBadgeItem class]]) {//提醒
        MYBadgeItem *badgeItem = (MYBadgeItem *)_item;
        self.badgeView.badgeValue = badgeItem.badgeValue;
        self.accessoryView = self.badgeView;
    }
    else if ([_item isKindOfClass:[MYSwitchItem class]]) {//开关
        self.accessoryView = self.switchView;
    }
    else if ([_item isKindOfClass:[MYCheakItem class]]) {//对号
        MYCheakItem *cheakItem = (MYCheakItem *)_item;
        
        if (cheakItem.isCheak) {
            self.accessoryView = self.cheakView;
        }
        else {
            self.accessoryView = nil;
        }
    }
    else {
        self.accessoryView = nil;
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.labelView.frame = self.bounds;
}
@end
